package viettravel.guiandevent.place;

import org.holoeverywhere.widget.Button;
import org.json.JSONObject;
import viettravel.DTO.DBCommunication;
import viettravel.DTO.JSONParser;
import viettravel.common.Constants;
import viettravel.guiandevent.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class PlaceDetailDialogComment extends Activity {

	@SuppressWarnings("unused")
	private Context mContext;
	private int RESULT_LOAD_IMAGE = 1;
	private int mPlaceId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// setTheme(R.style.Holo_Animation_Dialog);
		setContentView(R.layout.customview_comment_dialog);
		setTitle(getString(R.string.placedetail_dialogcomment_title));

		Bundle extra = getIntent().getExtras();
        if(extra == null)
        	this.finish();
        mPlaceId = extra.getInt(Constants.EXTRA_PLACE_ID);
        
		mContext = this;
		Button btnChooseImage = (Button) findViewById(R.id.placeDetailDialogCommentbtnChooseImage);
		btnChooseImage.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(
						Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(i, RESULT_LOAD_IMAGE);
			}
		});
		
		
		Button btnPostComment = (Button) findViewById(R.id.placeDetailDialogCommentCommitButton);
		btnChooseImage.setTypeface(Constants.TYPEFACE_NYALA);
		btnPostComment.setTypeface(Constants.TYPEFACE_NYALA);
		btnPostComment.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				final ImageView imageView = (ImageView) findViewById(R.id.placeDetailDialogCommentImage);
				final TextView textView = (TextView) findViewById(R.id.placeDetailDialogCommentCommentText);
				textView.setTypeface(Constants.TYPEFACE_NYALA);
				if (textView.getText() == null) {
					new org.holoeverywhere.app.AlertDialog.Builder(
							PlaceDetailDialogComment.this)
							.setTitle(
									R.string.placedetail_dialogcomment_alerttitle)
							.setMessage(
									R.string.placedetail_dialogcomment_alertmessage)
							.setPositiveButton(
									R.string.placedetail_dialogcomment_alertok,
									null).show();
				} else {
					postComment(Constants.CURRENT_FACEBOOK_USER.getName(),
							mPlaceId, imageView.getBackground(), textView
									.getText().toString());
					// finish();
				}
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
				&& null != data) {
			Uri selectedImage = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };
			Cursor cursor = getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
			if (cursor.moveToFirst()) {
				int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
				String picturePath = cursor.getString(columnIndex);
				cursor.close();
				Drawable d = Drawable.createFromPath(picturePath);
				ImageView i = (ImageView) findViewById(R.id.placeDetailDialogCommentImage);
				i.getLayoutParams().height = Constants.SLIDE_IMAGE_HEIGHT;
				i.getLayoutParams().height = Constants.SLIDE_IMAGE_WIDTH;
				d = Constants.reduceImage(d, Constants.SLIDE_IMAGE_WIDTH, Constants.SLIDE_IMAGE_HEIGHT);
				i.setBackgroundDrawable(d);
			}
		}
	}
	
	public void postComment(String userName, int placeId, Drawable drawable, String comment){
		String image = "";
		if(drawable!=null){
			byte[] imgbuf=Constants.drawable2ByteArray(drawable);
			image=Constants.byteArray2String(imgbuf);
		}
		JSONObject jsonObject=new JSONParser().makeCommentJson(placeId, comment, userName, image);
		Log.d("NEW PLACE", jsonObject.toString());
		new PostData().execute(jsonObject);
		
	}
	
	public class PostData extends AsyncTask<JSONObject, Void, Boolean> {

		private ProgressDialog mProgressDialog;

		@Override
		protected void onPreExecute() {
			mProgressDialog = new ProgressDialog(PlaceDetailDialogComment.this,
					org.holoeverywhere.app.ProgressDialog.THEME_HOLO_LIGHT);
			mProgressDialog.setMessage("Post data...");
			mProgressDialog.setCancelable(false);
			mProgressDialog.setIndeterminate(true);
			mProgressDialog.show();
		};


		
		@Override
		protected void onPostExecute(Boolean result){
			if (mProgressDialog.isShowing()) 
			{
				mProgressDialog.dismiss();
				finish();
			}
		}

		@Override
		protected Boolean doInBackground(JSONObject... params) {
			
			return DBCommunication.postData(Constants.ROOT, Constants.SUFFIX_POST_COMMENT,params[0] );
			
		}
	}

}
